/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.IllegalOrphanException;
import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Caracteriser;
import MODEL.Caracteristique;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class CaracteristiqueJpaController implements Serializable {

    public CaracteristiqueJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Caracteristique caracteristique) throws PreexistingEntityException, Exception {
        if (caracteristique.getCaracteriserCollection() == null) {
            caracteristique.setCaracteriserCollection(new ArrayList<Caracteriser>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Caracteriser> attachedCaracteriserCollection = new ArrayList<Caracteriser>();
            for (Caracteriser caracteriserCollectionCaracteriserToAttach : caracteristique.getCaracteriserCollection()) {
                caracteriserCollectionCaracteriserToAttach = em.getReference(caracteriserCollectionCaracteriserToAttach.getClass(), caracteriserCollectionCaracteriserToAttach.getCaracteriserPK());
                attachedCaracteriserCollection.add(caracteriserCollectionCaracteriserToAttach);
            }
            caracteristique.setCaracteriserCollection(attachedCaracteriserCollection);
            em.persist(caracteristique);
            for (Caracteriser caracteriserCollectionCaracteriser : caracteristique.getCaracteriserCollection()) {
                Caracteristique oldCaracteristiqueOfCaracteriserCollectionCaracteriser = caracteriserCollectionCaracteriser.getCaracteristique();
                caracteriserCollectionCaracteriser.setCaracteristique(caracteristique);
                caracteriserCollectionCaracteriser = em.merge(caracteriserCollectionCaracteriser);
                if (oldCaracteristiqueOfCaracteriserCollectionCaracteriser != null) {
                    oldCaracteristiqueOfCaracteriserCollectionCaracteriser.getCaracteriserCollection().remove(caracteriserCollectionCaracteriser);
                    oldCaracteristiqueOfCaracteriserCollectionCaracteriser = em.merge(oldCaracteristiqueOfCaracteriserCollectionCaracteriser);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCaracteristique(caracteristique.getIdCar()) != null) {
                throw new PreexistingEntityException("Caracteristique " + caracteristique + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Caracteristique caracteristique) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteristique persistentCaracteristique = em.find(Caracteristique.class, caracteristique.getIdCar());
            Collection<Caracteriser> caracteriserCollectionOld = persistentCaracteristique.getCaracteriserCollection();
            Collection<Caracteriser> caracteriserCollectionNew = caracteristique.getCaracteriserCollection();
            List<String> illegalOrphanMessages = null;
            for (Caracteriser caracteriserCollectionOldCaracteriser : caracteriserCollectionOld) {
                if (!caracteriserCollectionNew.contains(caracteriserCollectionOldCaracteriser)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Caracteriser " + caracteriserCollectionOldCaracteriser + " since its caracteristique field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Caracteriser> attachedCaracteriserCollectionNew = new ArrayList<Caracteriser>();
            for (Caracteriser caracteriserCollectionNewCaracteriserToAttach : caracteriserCollectionNew) {
                caracteriserCollectionNewCaracteriserToAttach = em.getReference(caracteriserCollectionNewCaracteriserToAttach.getClass(), caracteriserCollectionNewCaracteriserToAttach.getCaracteriserPK());
                attachedCaracteriserCollectionNew.add(caracteriserCollectionNewCaracteriserToAttach);
            }
            caracteriserCollectionNew = attachedCaracteriserCollectionNew;
            caracteristique.setCaracteriserCollection(caracteriserCollectionNew);
            caracteristique = em.merge(caracteristique);
            for (Caracteriser caracteriserCollectionNewCaracteriser : caracteriserCollectionNew) {
                if (!caracteriserCollectionOld.contains(caracteriserCollectionNewCaracteriser)) {
                    Caracteristique oldCaracteristiqueOfCaracteriserCollectionNewCaracteriser = caracteriserCollectionNewCaracteriser.getCaracteristique();
                    caracteriserCollectionNewCaracteriser.setCaracteristique(caracteristique);
                    caracteriserCollectionNewCaracteriser = em.merge(caracteriserCollectionNewCaracteriser);
                    if (oldCaracteristiqueOfCaracteriserCollectionNewCaracteriser != null && !oldCaracteristiqueOfCaracteriserCollectionNewCaracteriser.equals(caracteristique)) {
                        oldCaracteristiqueOfCaracteriserCollectionNewCaracteriser.getCaracteriserCollection().remove(caracteriserCollectionNewCaracteriser);
                        oldCaracteristiqueOfCaracteriserCollectionNewCaracteriser = em.merge(oldCaracteristiqueOfCaracteriserCollectionNewCaracteriser);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                BigDecimal id = caracteristique.getIdCar();
                if (findCaracteristique(id) == null) {
                    throw new NonexistentEntityException("The caracteristique with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(BigDecimal id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteristique caracteristique;
            try {
                caracteristique = em.getReference(Caracteristique.class, id);
                caracteristique.getIdCar();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The caracteristique with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Caracteriser> caracteriserCollectionOrphanCheck = caracteristique.getCaracteriserCollection();
            for (Caracteriser caracteriserCollectionOrphanCheckCaracteriser : caracteriserCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Caracteristique (" + caracteristique + ") cannot be destroyed since the Caracteriser " + caracteriserCollectionOrphanCheckCaracteriser + " in its caracteriserCollection field has a non-nullable caracteristique field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(caracteristique);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Caracteristique> findCaracteristiqueEntities() {
        return findCaracteristiqueEntities(true, -1, -1);
    }

    public List<Caracteristique> findCaracteristiqueEntities(int maxResults, int firstResult) {
        return findCaracteristiqueEntities(false, maxResults, firstResult);
    }

    private List<Caracteristique> findCaracteristiqueEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Caracteristique.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Caracteristique findCaracteristique(BigDecimal id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Caracteristique.class, id);
        } finally {
            em.close();
        }
    }

    public int getCaracteristiqueCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Caracteristique> rt = cq.from(Caracteristique.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
